/**
 * author bianlongting
 * email  1137060420@qq.com
 * date   2016-04-14
 * time   14:36
 * description 广告管理
 */

$(function(){
	//缓存节点
	var ad = {
		condition 		: $(".condition"),			//条件  1为选择专题，2为添加链接
		selectCondition : $(".select-condition select"),	//选择条件
		loginName		: $(".loginName"),			//幻灯片名称
		btnSave  		: $(".btn-save"),			//保存
		btnAdd 			: $(".btn-add"),			//添加
	};
	var pageIndex = 1;	//页码默认为第一页
	var pageCount;		//总页码
	//添加广告
	ad.condition.on("change",function(){
		var that = $(this);
		if (that.val() == 0) {
			selectTopic(0);
		}else if (that.val() == 1) {
			$(".select-condition").html("<input type = 'text'>");
		};
	});

    if (ad.condition.val() == 0) {
        selectTopic();
    }else if (ad.condition.val() == 1) {
        var dataId = ad.selectCondition.parent().data().id;
        if (dataId) {
            ad.selectCondition.parent().html("<input type = 'text' value = '" + dataId + "'>");
        }else{
            ad.selectCondition.parent().html("<input type = 'text'>");
        }
       
    };


    //获取主题
    function selectTopic(param){
        $.ajax({
            url : "/index.php?m=Home&c=Activity%2FCreateAdvert&a=ajaxGetTheme",
            type : "POST",
            dataType : "JSON",
            success : function(data){
                if (data.success == true) {
                    var html = "";
                    var data = data.data;
                    if (data.length > 0) {
                        if (param == 0) {
                            html += "<select>";
                            for (var i = 0; i < data.length; i++) {
                                html += "<option value = " + data[i].id + ">" + data[i].name + "</option>";
                            };
                            html += "</select>";
                            $(".select-condition").html(html);
                            return false;
                        };
                        for (var i = 0; i < data.length; i++) {
                            html += "<option value = " + data[i].id + ">" + data[i].name + "</option>";
                        };
                        ad.selectCondition.html(html);
                        if (ad.condition.val() == 0) {
                            $("#condition").val($(".select-condition").data().id);
                        }else if (ad.condition.val() == 1) {
                            ad.selectCondition.html("<input type = 'text' value = " + ad.selectCondition.parent().data().id + ">");
                        };
                    }else{
                        ad.selectCondition.html("");
                    }
                }else{
                    alert(data.message);
                }
            }
        })
    };

    //保存
    ad.btnSave.on("click",function(){
        var val = ad.loginName.val();
        if (val == "") {
            toast("幻灯片名称不能为空");
            return false;
        }else if (val.length < 6) {
            toast("幻灯片名称长度不得少于6位");
            return false;
        };

        //是否启用 1 启用 2 不启用
        var status = $("input[name='status']");
        if (status.prop("checked")) {
            status.val("1");
        }else{
            status.val("2");
        }
        if (ad.condition.val() == 0) {
            var topic =$(".select-condition").find("select").val();
        }else if (ad.condition.val() == 1) {
            var topic = $(".select-condition input").val();
        };
        $(".topic").val(topic);
        $("form").submit();
    });

    // 修改排序数字
    var sort =$('.input-slide-sort');
    sort.each(function(){
        $(this) .blur(function(){
            var sortNum = $(this).val(); // 值
            var linkId =  $(this).parents('tr').data().id; // linkid
            var data = {}
            data.linkId = linkId;
            data.sort = sortNum;
            $.ajax({
                url : '/index.php?m=Home&c=Activity%2FCreateAdvert&a=ajaxEditSort',
                data : data,
                type : 'POST',
                dataType : 'JSON',
                success : function(data){
                    if(data.success == true){
                        alert(data.message);
                    } else {
                        alert(data.message);
                    }
                }

            });
        })
    });

    // 删除广告
    var slideDelete = $('.btn-slide-delete');
    slideDelete.each(function(){
        $(this).on('click',function(){
            var  thisTr = $(this).parents('tr');
            var linkId = thisTr.data().id; // linkid
            var data = {};
            data.linkId = linkId;
            $('.btn-delete').on('click',function(){
                $('.close').trigger('click');
                $.ajax({
                    url : '/index.php?m=Home&c=Activity%2FCreateAdvert&a=ajaxEditDelete',
                    data : data,
                    type : 'POST',
                    dataType : 'JSON',
                    success : function(result){
                        if (result.success == true) {
                            thisTr.remove();
                        } else {
                            alert(result.message);
                        }

                    }
                });
            });
        });
    });

    // 状态暂停或者开启
    var slideStatus = $('.btn-slide-status');
    slideStatus.each(function(){
        $(this).on('click',function(){
            var  here = $(this);
            var linkId = here.parents('tr').data().id; // linkid
            var  statusName = $(this).html();
            var  finalName = '';
            var fianlShow = '';
            var data = {};
            var status = 0;
            if (statusName == '开启'){
                status = 1;
                fianlShow = '进行中';
                finalName = '暂停';

            } else {
                fianlShow = '已暂停';
                finalName = '开启';
            }
            data.linkId = linkId;
            data.status = status;
            $.ajax({
                url : '/index.php?m=Home&c=Activity%2FCreateAdvert&a=ajaxEditStatus',
                data : data,
                type : 'POST',
                dataType : 'JSON',
                success : function(result){
                    if (result.success == true) {
                        here.parents('tr').children('#show').html(fianlShow);
                        here.html(finalName);
                        if (status) {
                            here.removeClass('red').addClass('blue');
                        } else {
                            here.removeClass('blue').addClass('red');
                        }
                    } else {
                        alert(result.messgae);
                    }

                }
            });
        });

    });


  


    //toast提示函数
    function toast(tip){
        $(".toast_tip").html(tip);
        $(".toast").fadeIn('1000');
        setTimeout(function(){
            $(".toast").fadeOut();
        },1000);
        return false;
    }
})